import os.path
import time
import zj_toolkit
import wget
import shelve
import shutil
class COS:
    def __init__(self, initial_path=""):
        self.current_path = initial_path if initial_path else os.getcwd()
        self.commands = {
            "help": self.help,
            "dc": self.dc,
            "clear": self.clear,
            "cd": self.change_directory,
            "askzj": self.show_commands,
            "wget": self.wget_download,
            "rm": self.remove_files_or_dirs,
        }

    def run(self):
        while True:
            cmd = input(f"COS ({self.current_path}) > ")
            user = cmd.split()

            if len(user) > 0 and user[0] in self.commands:
                action = self.commands[user[0]]
                action(user[1:])
            else:
                print("未知命令，请输入 'help' 查看可用命令列表")

    def help(self, args):
        print("可用命令：")
        for command in self.commands:
            print(f"- {command}")

    def dc(self, args):
        print(' _______  _______           _______  _______      _______  _______  _______ ')
        print("(  ____ )(  ___  )|\     /|(  ____ \(  ____ )    (  ___  )(  ____ \(  ____ \ ")
        print('| (    )|| (   ) || )   ( || (    \/| (    )|    | (   ) || (    \/| (    \/')
        print('|| (____)|| |   | || | _ | || (__    | (____)|    | |   | || (__    | (__    ')
        print('|  _____)| |   | || |( )| ||  __)   |     __)    | |   | ||  __)   |  __)   ')
        print('| (      | |   | || || || || (      | (\ (       | |   | || (      | (      ')
        print('| )      | (___) || () () || (____/\| ) \ \__    | (___) || )      | )      ')
        print("|/       (_______)(_______)(_______/|/   \__/    (_______)|/       |/       ")
        time.sleep(0.5)
        exit(0)

    def clear(self, args):
        os.system('cls' if os.name == 'nt' else 'clear')  # 根据操作系统选择清屏命令

    def change_directory(self, args):
        try:
            target_path = os.path.join(self.current_path, args[0])
            os.chdir(target_path)
            self.current_path = os.getcwd()
        except Exception as e:
            print(e)
            print("请输入正确的目录或目录名")

    def show_commands(self, args):
        print('ls\ntimenow\nwget\ncd\ncolor\n')

    def wget_download(self, args):
        try:
            wget.download(args[0], self.current_path)
        except Exception as e:
            print(f'An error occurred: {e}')

    def remove_files_or_dirs(self, args):
        if len(args) == 2:
            file_or_dir = os.path.join(self.current_path, args[1])
            os.remove(file_or_dir)
        elif len(args) == 3 and args[1] == "-rf":
            dir_to_remove = os.path.join(self.current_path, args[2])
            shutil.rmtree(dir_to_remove)
        elif len(args) == 3 and args[1] == "-r":
            dir_to_remove = os.path.join(self.current_path, args[2])
            os.rmdir(dir_to_remove)
        else:
            print("使用 rm 命令时，请确保参数正确，如：rm <文件名>，rm -rf <目录名> 或 rm -r <空目录名>")


allpath = os.getcwd()
down = 'https://i-440.wwentua.com:446/01202000159054184bb/2024/01/20/c306a13e928748f57503d5b25475698c.7z?st=Ah3UANe4Ji7FlOw8Y1uK6Q&e=1705757596&b=Ax5aCQhlVDAAPF5mBCYOf1VuD31QZgt3&fi=159054184&pid=223-88-51-79&up=2&mp=0&co=0'
colist=["ls","wget","askzj","cd","dc"]


filenum = 0
mfile = ["zj_toolkit", "toolkit.py","__pycache__"]
path = os.getcwd()
for i in range(len(mfile)):
    if mfile[i] in os.listdir(path):
        print("Found_" + path+r"\"" +mfile[i])
        filenum += 1
        time.sleep(1)
time.sleep(2)
for i in range(10):
    print("creat_")
    for i in range(5):
        zj_toolkit.printProgress(i + 1, 5, "Creating", "room", 0, 30)

        i += 1
        time.sleep(0.1)
    print(zj_toolkit.hexman(100000000, 9999999999))
print('\n')
time.sleep(2)
os.system('cls')
print("Hello World!")
time.sleep(2)
zj_toolkit.cicon(False, 0)
time.sleep(2)
print("Let's begin")
time.sleep(2)

if os.path.exists("userinfo.bak")==False and os.path.exists("userinfo.dir")==False and os.path.exists("userinfo.dat")==False:
    usn=input("Who're you?:")
    passwd=input("passwd:")
    info={"name":usn,"password":passwd}
    with shelve.open('userinfo') as data:
        data['name'] = info["name"]
        data['password'] = info["password"]
else:
    with shelve.open('userinfo') as data:
        print("Welcome!",data["name"])
if __name__ == "__main__":
    cos = COS()
    cos.run()
'''
    user = input(os.getcwd() + ">>>:").split()
    if len(user) == 0:
        continue
    elif len(user) >= 1 and user[0]=="ls":
        print(os.listdir())

    elif len(user) == 0:
        continue

    elif user[0]=="clear":
        os.system('cls')

    elif user[0]=="cd":
            try:
                os.chdir(allpath+r"\\"+user[1])
            except Exception as r:
                try:
                    os.chdir(user[1])
                except Exception as e:
                    print(e)
                    print("请输入正确的目录或目录名")
    elif user[0] == "askzj":
        print('ls\ntimenow\nwget\ncd\ncolor\n')

    elif user[0] == "wget":
        try:
            wget.download(user[1], allpath)
        except Exception as e:
            print(f'An error occurred: {e}')
    elif  user[0] == "rm":
        if len(user)==2:
            os.remove(allpath+r"\\"+user[1])
        elif  len(user)==3 and user[1]=="-rmf":
            shutil.rmtree(allpath+r"\\"+user[2])
        elif len(user) == 3 and user[1] == "-rm":
            os.rmdir(allpath+r"\\"+user[2])

    elif user[0]=="dc":
        print(' _______  _______           _______  _______      _______  _______  _______ ')
        print("(  ____ )(  ___  )|\     /|(  ____ \(  ____ )    (  ___  )(  ____ \(  ____ \ ")
        print('| (    )|| (   ) || )   ( || (    \/| (    )|    | (   ) || (    \/| (    \/')
        print('|| (____)|| |   | || | _ | || (__    | (____)|    | |   | || (__    | (__    ')
        print('|  _____)| |   | || |( )| ||  __)   |     __)    | |   | ||  __)   |  __)   ')
        print('| (      | |   | || || || || (      | (\ (       | |   | || (      | (      ')
        print('| )      | (___) || () () || (____/\| ) \ \__    | (___) || )      | )      ')
        print("|/       (_______)(_______)(_______/|/   \__/    (_______)|/       |/       ")
        time.sleep(0.5)
        break
    else:
        print("War.!Unknow command please ask for zj or input askzj")
'''